<template>
  <DaprPlusTable
    :table-title="$t('fsb.games.point.list')"
    :entity="entity"
    :grid-options="gridOptions"
    :query-api="crudApi.getPage"
    :create-api="crudApi.create"
    :update-api="crudApi.update"
    :delete-api="crudApi.delete"
    :default-new-record="createDefaultPoint"
    @update:grid-api="onGridApiUpdate"
  />
</template>

<script lang="ts" setup>
import type { ExtendedVxeGridApi, VxeGridProps } from '#/adapter/vxe-table';
import { DaprPlusTable } from '#/components/daprplus';
import { createCrudApi } from '#/api/daprplus';
import type { IOutDto, IInDto, EntityInfo } from '#/utils/daprplus';
import { createBooleanColumn, createEnumColumn } from '#/utils/daprplus';
import { $t } from '#/locales';

const baseUrl = '/games/api/point';
const crudApi = createCrudApi<PointInDto, PointOutDto>(baseUrl);

const entity: EntityInfo = {
  code: 'Point',
  display: $t('fsb.games.point.title'),
};

enum PointType {
  Npc = 0,
  Monster = 1,
  Resource = 2,
  Task = 3,
  Caokuang = 4,
  Trans = 5,
}

// 验证规则
const validRules = {
  name: [
    { required: true, message: '坐标名称不能为空' },
    { min: 2, max: 50, message: '坐标名称长度应在2-50个字符之间' },
  ],
  x: [{ required: true, message: 'X坐标不能为空' }],
  y: [{ required: true, message: 'Y坐标不能为空' }],
  gameMapId: [{ required: true, message: '地图ID不能为空' }],
  remark: [{ max: 200, message: '备注长度应在0-200个字符之间' }],
};

const pointTypeLabels = {
  [PointType.Npc]: 'NPC',
  [PointType.Monster]: '怪物',
  [PointType.Resource]: '资源',
  [PointType.Task]: '任务',
  [PointType.Caokuang]: '草药矿物',
  [PointType.Trans]: '传送',
};

// 扩展配置
const gridOptions: VxeGridProps<PointOutDto> = {
  editRules: validRules,
  columns: [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '编号',
      sortable: true,
      width: 130,
    },
    {
      field: 'name',
      title: '坐标名称',
      sortable: true,
      align: 'left',
      width: 150,
      editRender: { name: 'input' },
    },
    {
      field: 'x',
      title: '坐标X',
      sortable: true,
      width: 110,
      editRender: { name: 'input', props: { type: 'number' } },
    },
    {
      field: 'y',
      title: '坐标Y',
      sortable: true,
      width: 110,
      editRender: { name: 'input', props: { type: 'number' } },
    },
    {
      field: 'runDistance',
      title: '偏移',
      sortable: true,
      width: 86,
      editRender: { name: 'input', props: { type: 'number' } },
    },
    {
      field: 'clickX',
      title: '点击X',
      sortable: true,
      width: 100,
      editRender: { name: 'input', props: { type: 'number' } },
    },
    {
      field: 'clickY',
      title: '点击Y',
      sortable: true,
      width: 100,
      editRender: { name: 'input', props: { type: 'number' } },
    },
    {
      field: 'order',
      title: '顺序',
      sortable: true,
      width: 86,
      editRender: { name: 'input', props: { type: 'number' } },
    },
    createEnumColumn<PointType>('pointType', '坐标类型', pointTypeLabels),
    createBooleanColumn('isRun', '寻路', { width: 90 }),
    createBooleanColumn('isLocked', '锁定', { width: 90 }),
    {
      field: 'gameMapId',
      title: '地图ID',
      sortable: true,
      width: 111,
      editRender: { name: 'input' },
    },
    {
      field: 'createdTime',
      title: '创建时间',
      formatter: 'formatDateTime',
      width: 140,
    },
    {
      field: 'remark',
      title: '备注',
      align: 'left',
      minWidth: 100,
      editRender: { name: 'input' },
    },
  ],
};

let gridApi: ExtendedVxeGridApi;

function onGridApiUpdate(api: ExtendedVxeGridApi) {
  gridApi = api;
}

function createDefaultPoint(): PointInDto {
  return {
    id: '0',
    name: '新坐标点',
    x: 0,
    y: 0,
    runDistance: 0,
    clickX: 0,
    clickY: 0,
    order: 0,
    pointType: PointType.Npc,
    remark: '',
    isRun: false,
    isLocked: false,
    gameMapId: '0',
  };
}

export interface PointOutDto extends IOutDto {
  name?: string;
  x?: number;
  y?: number;
  runDistance?: number;
  clickX?: number;
  clickY?: number;
  order?: number;
  pointType?: PointType;
  isRun?: boolean;
  isLocked?: boolean;
  gameMapId?: string;
  remark?: string;
  createdTime?: Date;
}

export interface PointInDto extends IInDto {
  name: string;
  x: number;
  y: number;
  runDistance: number;
  clickX: number;
  clickY: number;
  order: number;
  pointType: PointType;
  isRun: boolean;
  isLocked: boolean;
  gameMapId: string;
  remark: string;
}
</script>

<style scoped></style>
